#include <stdio.h>
#define BUF_LEN 1024*20
#define BUF_SZ 4000

extern gi_stype;
extern gi_rtype;

main(int argc, char *argv[])
{
	int ret;
	FILE *fp_in, *fp_out;
	char in_file_name 	[1024];
	char out_file_name 	[1024];
	char svc_name 		[1024];
	char input_buff		[BUF_LEN];
	char output_buff	[BUF_LEN];
	int conv_type;

	int read_size;
	int write_size;

	gi_stype = 2;
	gi_rtype = 2;

	conv_type = atoi( argv[1]);
	strcpy( svc_name, argv[2]);
 
	sprintf(in_file_name, "data/%s", svc_name);

	if(conv_type == 1) {
		sprintf(out_file_name, "data/%s.e", svc_name);
		printf("** ASCII to EBCDIC (%s->%s)\n", svc_name, out_file_name);
	} else {
		sprintf(out_file_name, "data/%s.a", svc_name);
		printf("** EBCDIC to ASCII (%s->%s)\n", svc_name, out_file_name);
	}

	/* ------------------------------------------------------------------ */
#if 1
	fp_in = fopen( in_file_name, "rb" );
	fp_out = fopen( out_file_name, "wb" );

	if(fp_in == NULL){
		fprintf(stderr,"input file open err\n");
		exit(0);
	}
	if(fp_out == NULL){
		fprintf(stderr,"output file open err\n");
		exit(0);
	}

	memset( input_buff, 0, BUF_LEN);
	memset( output_buff, 0, BUF_LEN);
	read_size = fread((char *)input_buff, 1, (int)BUF_LEN, (FILE *)fp_in);
	printf("-- ReadSize:%d\n", read_size);
	HexLog( input_buff, read_size );
#endif

	if(conv_type == 1)
		write_size = Ascii2EbcdicStr( input_buff, output_buff, read_size );
	else
		write_size = Ebcdic2AsciiStr( input_buff, output_buff, read_size );

	write_size = strlen( output_buff );
	printf("-- WriteSize:%d\n", write_size);
	HexLog( output_buff, write_size );
	fwrite((char *)output_buff, 1, write_size, (FILE *)fp_out);

	if(fp_in) fclose(fp_in);
	if(fp_out) fclose(fp_out);

	return(0);
}
